<template>
  <div class="addEmployee">
    <h3 class="title">注册员工信息</h3>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="150px"
      class="demo-ruleForm"
    >
      <el-form-item label="员工名称" prop="userName">
        <el-input v-model="ruleForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="员工年龄" prop="age">
        <el-input v-model="ruleForm.age"></el-input>
      </el-form-item>
      <el-form-item label="员工性别" prop="sex">
        <el-radio-group v-model="ruleForm.sex">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="员工密码" prop="passWord">
        <el-input v-model="ruleForm.passWord"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :plain="true" @click="submitForm('ruleForm')">注册</el-button>
        <el-button type="info" :plain="true" @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import adminRequest from "../request/admin";
import cookie from "js-cookie";
export default {
  data() {
    var userName = (rule, value, callback) => {
      if (value === "") {
        return callback(new Error("请填写员工名称"));
      } else {
        return callback();
      }
    };
    var age = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写员年龄"));
      } else {
        if (/^(([0-9]|[1-9][1-9]|1[0-7][0-9])(\\.[0-9]+)?|180)$/.test(value)) {
          return callback();
        } else {
          return callback(new Error("请输入合法年龄"));
        }
      }
    };
    var sex = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请选择员工性别"));
      } else {
        return callback();
      }
    };
    var passWord = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请设置员工密码"));
      } else {
        return callback();
      }
    };

    var phone = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请填写员工电话"));
      } else {
        if (/^1[3|4|5|8][0-9]\d{4,8}$/.test(value)) {
          return callback();
        } else {
          return callback(new Error("请输入正确的手机格式"));
        }
      }
    };

    return {
      ruleForm: {
        userName: "", //客户名称
        age: "", //出生日期
        sex: "", //性别
        passWord: "", //员工密码
        phone: "" //电话
      },
      rules: {
        userName: [{ validator: userName, trigger: "blur" }],
        age: [{ validator: age, trigger: "blur" }],
        sex: [{ validator: sex, trigger: "blur" }],
        passWord: [{ validator: passWord, trigger: "blur" }],
        phone: [{ validator: phone, trigger: "blur" }]
      }
    };
  },

  methods: {
    // 注册员工
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          adminRequest
            .addEmployee({
              userName: this.ruleForm.userName,
              age: this.ruleForm.age,
              sex: this.ruleForm.sex,
              passWord: this.ruleForm.passWord,
              phone: this.ruleForm.phone
            })
            .then(res => {
              if (res.data.success == true) {
                this.$message.success("添加成功!");
                this.resetForm();
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查信息是否完整！");
          return false;
        }
      });
    },
    // 清空表单
    resetForm() {
      this.$refs.ruleForm.resetFields();
    }
  },

  created() {}
};
</script>

<style lang="scss" scoped>
.addEmployee {
  overflow: hidden;

  ::v-deep .title {
    text-align: center;
    font-size: 20px;
  }
  ::v-deep .demo-ruleForm {
    width: 500px;
    margin: 30px auto;
  }
  ::v-deep .el-input__inner {
    height: 45px;
    font-size: 16px;
  }
  ::v-deep .el-form-item__label {
    font-size: 16px;
    position: relative;
    top: 10px;
  }
  ::v-deep .el-form-item__content {
    margin-top: 10px;
    height: 50px;
    display: flex;
  }
  ::v-deep .el-form-item__error {
    font-size: 16px;
  }

  ::v-deep .el-select-dropdown__item {
    display: flex;
    justify-content: center;
  }
  ::v-deep .el-radio-group {
    width: 100px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 11px;
  }
}
</style>